package com.kun.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.kun.controller.BaseController;
import com.kun.entity.Activity;
import com.kun.result.ExceptionMsg;
import com.kun.result.Response;
import com.kun.service.ActivityService;
import com.kun.util.UploadCosUtil;

/**
 * @description: 
 * @author: KM
 * 2018年5月9日 下午4:42:48
 */
@Controller
@RequiresAuthentication
@RequestMapping("/admin")
public class ActivityAdminController extends BaseController{
	
	@Autowired
	private ActivityService activityService;
	
	@Value("${application.imgSuffix}")
    private String[] imgSuffix;
	
	
	//活动列表页面
	@RequiresPermissions("activityInfo:view")
	@RequestMapping("/activityList")
	public String activityList(){
		return "admin/activityList";
	}
	
	//活动列表
	@RequiresPermissions("activityInfo:view")
	@RequestMapping("/getActivity")
	@ResponseBody
	public List<Activity> getActivity(){
		return activityService.selectAllActivity();
	}
	
	//首页活动列表
	@RequestMapping("/getAdminIndexActivity")
	@ResponseBody
	public List<Activity> getAdminIndexActivity(){
		Map<String,Object> map = activityService.selectActivityTiltle(1, 5, "{issueTime:'desc'}", true);
		@SuppressWarnings("unchecked")
		List<Activity> list = (List<Activity>) map.get("activityList");
		return  list;
	}
	
	//修改活动普通信息
	@RequiresPermissions("activityInfo:update")
	@RequestMapping(value="/updateSimpleActivity",method=RequestMethod.POST)
	@ResponseBody
	public Response updateSimpleActivity(Activity activity){
		int i = activityService.updateSimpleActivityByKey(activity);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}else{
			return result(ExceptionMsg.FAILED);
		}
	}
		
	//修改活动内容信息
	@RequiresPermissions("activityInfo:update")
	@RequestMapping(value="/updateActivity",method=RequestMethod.POST)
	@ResponseBody
	public Response updateActivity(Activity activity){
		int i = activityService.updateActivityByKey(activity);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}else{
			return result(ExceptionMsg.FAILED);
		}
	}
	
	//新增活动
	@RequiresPermissions("activityInfo:add")
	@RequestMapping(value="/insertActivity",method=RequestMethod.POST)
	@ResponseBody
	public Response insertActivity(Activity activity){
		int i = activityService.insertActivity(activity);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}else{
			return result(ExceptionMsg.FAILED);
		}
	}
	
	//新增活动页面
	@RequiresPermissions("activityInfo:add")
	@RequestMapping("/toInsertActivity")
	public String toInsertActivity(){
		return "admin/newsAdd";
	}
	
	//更新活动页面
	@RequiresPermissions("activityInfo:update")
	@RequestMapping("/toUpdateActivity")
	public String toUpdateActivity(int id,Model model){
		if(id!=0){
			Activity activity = activityService.selectActivityByKey(id);
			model.addAttribute("activity", activity);
		}
		return "admin/newUpdate";
	}
	
	//删除活动
	@RequiresPermissions("activityInfo:delete")
	@RequestMapping(value="/deleteActivity",method=RequestMethod.POST)
	@ResponseBody
	public Response deleteActivity(int id){
		int i = activityService.deleteActivityByKey(id);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}else{
			return result(ExceptionMsg.FAILED);
		}
	}
	
	//文章图片处理
	@RequestMapping(value="uploadActivityImg",method=RequestMethod.POST) 
	@ResponseBody
    public Map<String,Object> uploadActivityImg(MultipartHttpServletRequest multiReq){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> data = new HashMap<String,Object>();
		try {
			MultipartFile  file = multiReq.getFile("file");
			if (file.isEmpty()) {  
				map.put("code", 000400);
				map.put("msg", "上传文件为空");
				return map;
	        }
			// 获取文件名  
	        String fileName = file.getOriginalFilename();  
	        logger.info("上传的文件名为：" + fileName);  
	        // 获取文件的后缀名  
	        String suffixName = fileName.substring(fileName.lastIndexOf("."));  
	        logger.info("上传的后缀名为：" + suffixName);
	        if(!ArrayUtils.contains(imgSuffix, suffixName)){
	        	//不支持的后缀
	        	map.put("code", 000403);
				map.put("msg", "图片格式必须为'jpg'、'png'、'jpge'、'gif'、'bmp'");
				return map;
	        }
	        String fName = UUID.randomUUID().toString() + suffixName;
			logger.info("活动图片上传： newname：" + fName);
			String cosPath = "/activityImg/";
			String url = UploadCosUtil.picCOS(file, cosPath, fName);
	        map.put("code", 0);
			map.put("msg", "");
			data.put("src", url);
			data.put("title", fName);
			map.put("data", data);
			return map;
		} catch (Exception e) {
			logger.error("failure--"+e.getMessage());
			map.put("code", 1);
			map.put("msg", "上传发生错误");
			return map;
		}
    }

}
